package ro.xblue.translator;

import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;


/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: InitLangModuleTag.java,v 1.1 2004/10/24 23:37:02 mihaipostelnicu Exp $
 */
public class InitLangModuleTag extends TagSupport {

      private String module;
      private String lang;

      public void setLang ( String lang ) {
            this.lang = lang;
      }

      public void setModule ( String module ) {
            this.module = module;
      }

      public String getLang () {
            return lang;
      }

      public String getModule () {
            return module;
      }

      public void removeValue ( String s ) {
//            System.out.println( "remove:" + s );
            super.removeValue( s );
      }

      public int doStartTag () throws JspException {
            return SKIP_BODY;
      }

      public int doEndTag () throws JspException {
            if ( module == null ) {
                  return EVAL_PAGE;
            }
            pageContext.setAttribute( PNAME_MODULE_NAME, module );

            ServletContext application = pageContext.getServletContext();

//            Translator translator = ( Translator ) application.
//                  getAttribute( TranslatorConstants.BLUE_TRANSLATOR );
            Translator_DB translator_db = null;
//            if ( translator == null )
//            {
            translator_db = ( Translator_DB ) application.
                  getAttribute( TranslatorConstants.DB_TRANSLATOR );
//            }

            if ( lang == null ) {
                  LanguageBean bean = TranslatorUtils.getCurrentLanguage( ( HttpServletRequest ) pageContext.getRequest(),
                        application );
                  lang = bean.language;
            }

//            if ( translator != null )
//            {
//                  Module _module = translator.getModule( module );
//                  if ( _module != null )
//                  {
//                        Properties props = _module.getMessages( lang );
//                        if ( props != null )
//                        {
//                              pageContext.setAttribute( PNAME_BUNDLE, props );
//                        }
//                  }
//            }
//            else
//            {
            Module_DB db_module = translator_db.getModule( module );
            if ( db_module != null ) {
                  pageContext.setAttribute( PNAME_MODULE, db_module );
                  Hashtable messages = db_module.getMessages( lang );
                  if ( messages != null ) {
                        pageContext.setAttribute( PNAME_BUNDLE, messages );
                  } else {
                        pageContext.setAttribute( PNAME_BUNDLE, EMPTY_HASHTABLE );
                  }
            } else {
                  pageContext.setAttribute( PNAME_BUNDLE, EMPTY_HASHTABLE );
            }
//            }

            // daca-s cacheute tagurile ...
            this.lang = null;
            this.module = null;

            return EVAL_PAGE;
      }

      public static final String PNAME_BUNDLE = "ILMT:PAGE_MSGS";
      public static final String PNAME_MODULE_NAME = "ILMT:PAGE_MODULE_NAME";
      public static final String PNAME_MODULE = "ILMT:PAGE_MODULE";
      private static final Hashtable EMPTY_HASHTABLE = new Hashtable();
}
